package com.cc.controller;

import com.cc.entity.User;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/user")
@ResponseBody
public class UserController {

    @RequestMapping(value = "/save", method = RequestMethod.GET, produces = "application/json;charset=utf-8")
    @ResponseBody
    public String save() {
        String msg = "用户保存方法";
        System.out.println(msg);
        return msg;
    }

    @RequestMapping(value = "/getParam", method = RequestMethod.GET, produces = "application/json;charset=utf-8")
    @ResponseBody
    public String param(String name, Integer age) {
        System.out.println(name);
        System.out.println(age);
        return name;
    }

    //接收单参数
    @GetMapping(value = "/paramOne", produces = "application/json;charset=utf-8")
    @ResponseBody
    public String paramOne(@RequestParam("userName") String name) {
        System.out.println("接收普通单参数name:" + name);
        return "单参数接收";
    }

    //接收多参数
    @GetMapping(value = "/paramList", produces = "application/json;charset=utf-8")
    @ResponseBody
    public String paramList(String name, int age) {
        System.out.println("接收普通单参数name:" + name);
        System.out.println("接收普通参数age:" + age);
        return "Get接收多参数";
    }

    //Post
    @GetMapping(value = "/paramPost", produces = "application/json;charset=utf-8")
    @ResponseBody
    public String paramPost(String name, int age) {
        System.out.println("接收Post参数name:" + name);
        System.out.println("接收Post参数age:" + age);
        return "post请求参数接收";
    }

    //接收pojo数据参数
    @PostMapping(value = "/paramPojoPost", produces = "application/json;charset=utf-8")
    @ResponseBody
    public String paramPojoPost(User user) {
        System.out.println(user.toString());
        System.out.println(user.getRole().toString());
        return "接收pojo数据参数";
    }

    //接收数组参数
    @PostMapping("/paramArray")
    @ResponseBody
    public String paramArray(String[] hobbies) {
        Arrays.stream(hobbies).forEach(h -> System.out.println(h));
        return "接收数组参数";
    }

    //接收集合数据
    @PostMapping(value = "/paramsList", produces = "application/json;charset=utf-8")
    @ResponseBody
    public String paramsList(List<String> hobbies) {
        hobbies.forEach(h-> System.out.println(h));
        return "接收集合参数";
    }

    //传递json对象
@PostMapping(value = "/jsonParam",produces = "application/json;charset=utf-8")
@ResponseBody
    public String jsonParam(@RequestBody User user){
        //@RequestBody用于接收json数据
        System.out.println(user);
        System.out.println(user.getRole());
        return "json对象接收";
    }

    //接收对象数组
    @PostMapping("/jsonList")
    @ResponseBody
    public String jsonList(@RequestBody List<User> users){
        users.stream().forEach(user -> System.out.println(user));
        return "json对象数组";
    }

    //传递日期格式
    @GetMapping("/dateParam")
    public String dateParam(@DateTimeFormat(pattern = "yyyy-MM-dd") Date dateTime,
                            Date date1,
                            @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date date2){
        System.out.println("接收yyyy-MM-dd:"+dateTime);
        System.out.println("接收yyyy/MM/dd:"+date1);
        System.out.println("接收yyyy-MM-dd hh:mm:ss:"+date2);
        return "接收日期格式参数";
    }


}

